<?php

namespace common\models\searchs;

use Yii;
use yii\base\Model;
use yii\data\ActiveDataProvider;
use common\models\DhCommonmodel;
use yii\db\Query;

/**
 * SearchDhCommonmodel represents the model behind the search form about `common\models\DhCommonmodel`.
 */
class SearchDhCommonmodel extends DhCommonmodel
{
    /**
     * @inheritdoc
     */
    public function rules()
    {
        return [
            [['GeneralID', 'NodeID', 'ModelID', 'stick','img_id', 'ItemID', 'Hits', 'DayHits', 'WeekHits', 'MonthHits', 'LinkType', 'Status', 'EliteLevel', 'Priority', 'CommentAudited', 'CommentUnAudited', 'SigninType', 'ShowCommentLink', 'SGType', 'SGDataId'], 'integer'],
            [['TableName', 'Title','subtitle', 'address','address_name', 'Inputer', 'UpdateTime', 'CreateTime', 'TemplateFile', 'InputTime', 'PassedTime', 'Editor', 'LastHitTime', 'DefaultPicUrl', 'PinyinTitle', 'TitleFontColor', 'TitleFontType', 'IncludePic', 'HtmlPageName'], 'safe'],
            [['TitleHashKey'], 'number'],
        ];
    }

    /**
     * @inheritdoc
     */
    public function scenarios()
    {
        // bypass scenarios() implementation in the parent class
        return Model::scenarios();
    }

    /**
     * Creates data provider instance with search query applied
     *
     * @param array $params
     *
     * @return ActiveDataProvider
     */
    public function search($params)
    {   
        $condition = '';
        $nodes = \common\models\DhNodes::findOne(["NodeID"=>$params['nodeid']]);
        if($params['nodeid'] == 10 || $nodes->ParentID == 10){
            $data  = (new Query())->select('*')->from("dh_nodes")->Where(['ParentID'=>10])->all();
            $category = '';
            foreach ($data as $value) {
                $category .= $value['NodeID'] . ',';
            }
            $category = substr($category, 0, -1);
            $condition = $category;
            $condition = 'NodeID in(' . $condition . ')';
            
        }else{
            $condition = $params['nodeid'];
            $condition = 'NodeID in(' . $condition . ')';
        }
        
        $query = DhCommonmodel::find()->where($condition)->orderBy('stick DESC,(case  when  Priority IS NULL  then 99999999 else Priority end),Priority ASC,UpdateTime DESC');

        $dataProvider = new ActiveDataProvider([
            'query' => $query,
        ]);

        if (!($this->load($params) && $this->validate())) {
            return $dataProvider;
        }

        $query->andFilterWhere([
            'GeneralID' => $this->GeneralID,
            'NodeID' => $this->NodeID,
            'ModelID' => $this->ModelID,
            'ItemID' => $this->ItemID,
            'Hits' => $this->Hits,
            'DayHits' => $this->DayHits,
            'WeekHits' => $this->WeekHits,
            'MonthHits' => $this->MonthHits,
            'LinkType' => $this->LinkType,
            'UpdateTime' => $this->UpdateTime,
            'CreateTime' => $this->CreateTime,
            'Status' => $this->Status,
            'EliteLevel' => $this->EliteLevel,
            'Priority' => $this->Priority,
            'CommentAudited' => $this->CommentAudited,
            'CommentUnAudited' => $this->CommentUnAudited,
            'SigninType' => $this->SigninType,
            'InputTime' => $this->InputTime,
            'PassedTime' => $this->PassedTime,
            'LastHitTime' => $this->LastHitTime,
            'ShowCommentLink' => $this->ShowCommentLink,
            'TitleHashKey' => $this->TitleHashKey,
            'SGType' => $this->SGType,
            'SGDataId' => $this->SGDataId,
            'img_id' => $this->img_id,
            'stick' => $this->stick,
            'address' =>$this->address,
            'address_name' =>$this->address_name,
            'subtitle' =>$this->subtitle,
        ]);

        $query->andFilterWhere(['like', 'TableName', $this->TableName])
            ->andFilterWhere(['like', 'Title', $this->Title])
            ->andFilterWhere(['like', 'subtitle', $this->subtitle])
            ->andFilterWhere(['like', 'Inputer', $this->Inputer])
            ->andFilterWhere(['like', 'TemplateFile', $this->TemplateFile])
            ->andFilterWhere(['like', 'Editor', $this->Editor])
            ->andFilterWhere(['like', 'DefaultPicUrl', $this->DefaultPicUrl])
            ->andFilterWhere(['like', 'PinyinTitle', $this->PinyinTitle])
            ->andFilterWhere(['like', 'TitleFontColor', $this->TitleFontColor])
            ->andFilterWhere(['like', 'TitleFontType', $this->TitleFontType])
            ->andFilterWhere(['like', 'IncludePic', $this->IncludePic])
            ->andFilterWhere(['like', 'address', $this->address])
            ->andFilterWhere(['like', 'address', $this->address_name])
            ->andFilterWhere(['like', 'HtmlPageName', $this->HtmlPageName]);

        return $dataProvider;
    }
}
